package com.ocom.accesservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ocom.accesservice.dao.IDaoWhiteInfo;
import com.ocom.accesservice.service.WhiteInfoService;
import com.ocom.common.entity.access.EntityMjWhiteInfo;
import com.ocom.common.enums.DataSourceEnum;
import com.ocom.common.utils.StringUtils;
import com.ocom.common.vo.DelAreaVo;
import com.ocom.mysql.aspect.DataSourceAspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;


@Service
public class WhiteInfoServiceImpl extends ServiceImpl<IDaoWhiteInfo, EntityMjWhiteInfo> implements WhiteInfoService {


    @Autowired
    private IDaoWhiteInfo daoWhiteInfo;

    /**
     * 清除门禁白名单
     * */
    @DataSourceAspect(type= DataSourceEnum.PART_DB,companyId = "companyID")
    @Transactional
    @Override
    public void RemoveArea(DelAreaVo delAreaVo,Long companyID) {
        QueryWrapper<EntityMjWhiteInfo> query = Wrappers.query();
        if (StringUtils.isNotNull(delAreaVo.getComId())){
            query.eq("com_id",delAreaVo.getComId());
        }
        if (StringUtils.isNotNull(delAreaVo.getAreaId())){
            query.eq("area_id",delAreaVo.getAreaId());
        }
        daoWhiteInfo.delete(query);

    }

    @DataSourceAspect(type= DataSourceEnum.PART_DB)
    @Override
    public boolean delInfo(Long companyId, Long areaNo, Long pId) {
        QueryWrapper<EntityMjWhiteInfo> query = Wrappers.query();
        query.eq("com_id",companyId);
        query.eq("area_id",areaNo);
        query.eq("p_id",pId);
        return remove(query);
    }

    @DataSourceAspect(type= DataSourceEnum.PART_DB)
    @Override
    public EntityMjWhiteInfo addInfo(Long companyId, Long areaNo, Long pId,Long state) {
        EntityMjWhiteInfo entityMjWhiteInfo=new EntityMjWhiteInfo();
        entityMjWhiteInfo.setComId(companyId);
        entityMjWhiteInfo.setPId(pId);
        entityMjWhiteInfo.setAreaId(areaNo);
        entityMjWhiteInfo.setState(state);
        entityMjWhiteInfo.setCreateTime(new Date());
        this.save(entityMjWhiteInfo);
        return entityMjWhiteInfo;
    }

    @DataSourceAspect(type= DataSourceEnum.PART_DB)
    @Override
    public boolean cleanInfo(Long companyId, Long areaNo) {
        QueryWrapper<EntityMjWhiteInfo> query = Wrappers.query();
        query.eq("com_id",companyId);
        query.eq("area_id",areaNo);
        return remove(query);
    }


}
